﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entities;
using Datalayer;
using Common;

namespace Portal_KonTum.Admin
{
    public partial class CatalogyNew : System.Web.UI.Page
    {
        IQueryMgr iq;
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["CateID"] = null;
            Session["CateID"] = Request.QueryString["CateID"];

            if (!IsPostBack)
            {
                iq = QueryMgr.GetDbInstance();
                List<Menus> menu = iq.Menu_GetAllMenu();
                DL_Menu.DataSource = menu;
                DL_Menu.DataValueField = "MenuID";
                DL_Menu.DataTextField = "MenuName";
                DL_Menu.DataBind();

                List<Category> iden = iq.Ca_GetAllCate();
                DL_Parent.DataSource = iden;
                DL_Parent.DataValueField = "CateID";
                DL_Parent.DataTextField = "CategoryName";
                DL_Parent.DataBind();


                if (Session["CateID"] == null)
                {

                }
                else
                {
                    bin_edit();
                    bt_save.Text = "Save";
                }
            }
        }

        protected void bt_save_Click(object sender, EventArgs e)
        {
            iq = QueryMgr.GetDbInstance();
            
            if (Session["CateID"] == null)
            {
                // thêm mới
                iq.Ca_NewCate(set_Cate());
                Response.Redirect("CatalogyMn.aspx?Notes=StrNew&Active=true");
            }
            else
            {
                // sua doi
                Guid gd = MethodCommon.Get_Guid(Session["CateID"].ToString());
                iq.Ca_EditCategory(set_Cate(gd));
                Response.Redirect("CatalogyMn.aspx?Notes=StrEdit&Active=true");
            }
            
        }

        protected void bt_cancel_Click(object sender, EventArgs e)
        {

        }

        void Fill_DataEdit(Category bg)
        {
            txt_name.Text = bg.CategoryName;
            txt_link.Text = bg.Links;
            DL_Menu.SelectedValue = bg.MenuID.ToString();
            DL_Parent.SelectedValue = bg.CateParentID.ToString();
        }

        void bin_edit()
        {
            Category bg = new Category();
            iq = QueryMgr.GetDbInstance();
            bg = iq.Ca_GetCateByID(new Guid(Session["CateID"].ToString()));
            Fill_DataEdit(bg);
        }

        /// <summary>
        /// Sửa chữa bug
        /// </summary>
        /// <param name="bg"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        Category set_Cate(Guid id)
        {
            Category bg = new Category();

            if (id != null) //sửa chữa
                bg.CateID = id;

            bg.CategoryName = txt_name.Text;
            bg.Links = txt_link.Text;
            bg.IsDeleted = false;
            if (CheckBox1.Checked == true)
            {
                bg.CateParentID = MethodCommon.Get_Guid(DL_Parent.SelectedValue.ToString());
                iq.Ca_HaveParent(bg);
            }
            else
                iq.Ca_NullParent(bg);
            bg.MenuID = MethodCommon.Get_Guid(DL_Menu.SelectedValue.ToString());
            
            return bg;
        }

        /// <summary>
        /// thêm mới
        /// </summary>
        /// <param name="bg"></param>
        /// <returns></returns>
        Category set_Cate()
        {
            Category bg = new Category();
            bg.CategoryName = txt_name.Text;
            bg.Links = txt_link.Text;
            bg.IsDeleted = false;
            if(CheckBox1.Checked==true)
                bg.CateParentID = MethodCommon.Get_Guid(DL_Parent.SelectedValue.ToString());
            bg.MenuID = MethodCommon.Get_Guid(DL_Menu.SelectedValue.ToString());
            
            bg.CateID = Guid.NewGuid();
            return bg;
        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if(CheckBox1.Checked)
                DL_Parent.Enabled = true;
            else
                DL_Parent.Enabled = false;
        }
    }
}